package com.example.myapplication;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
Button btn;
private JsonArray jsonArray;
List <Url> list=new ArrayList<>();
DialogAdapter dialogAdapter;
ListView listView;

    private Handler handler=new Handler(){
        public void  handleMessage(Message msg) {
            switch (msg.what) {
                case 1:


      dialogAdapter.notifyDataSetChanged();



                    break;

                case 567:

                    Toast.makeText(getApplicationContext(),"无数据,请检查是否输入正确设备号",Toast.LENGTH_SHORT).show();

                    break;

                default:
                    break;
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=findViewById(R.id.btn);
        listView=findViewById(R.id.listview);
dialogAdapter=new DialogAdapter(getApplicationContext(),list);
        listView.setAdapter(dialogAdapter);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
zz();
            }
        });
    }

    public  void zz(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client=new OkHttpClient();

                    Request request=new Request.Builder()
                            //.url("http://192.168.8.100:7001/api/toolStatus/"+deviceid)
                            .url("http://www2.boohersmart.com:3000/api/tool/select/basic/"+"031908002")
                            .build();
                    Response response=client.newCall(request).execute();
                    String data=response.body().string();
                    JsonObject jsonObject=new JsonParser().parse(data).getAsJsonObject();

                    jsonArray=jsonObject.getAsJsonArray("data");
                    if (jsonArray.size()<1){

                        Message message = new Message();
                        message.what = 567;
                        handler.sendMessage(message);



                    }else {
                        Gson gson = new Gson();


                        for (JsonElement post : jsonArray) {
                            Url u = gson.fromJson(post, new TypeToken<Url>() {
                            }.getType());
                            list.add(u);
                        }


                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    }

                }catch (Exception e){
                    e.printStackTrace();

                }
            }
        }).start();
    }

    private  void  xx(){
        dialogAdapter=new DialogAdapter(getApplicationContext(),list);
    }
}
